/**
 * 
 */
package ca.uwindsor.cs.deepweb.estimation.experiment;

import java.io.File;
import java.io.FileInputStream;
import java.io.IOException;
import java.io.InputStreamReader;
import java.util.Date;
import java.util.HashSet;
import java.util.Random;
import java.util.Set;

import org.apache.lucene.analysis.Analyzer;
import org.apache.lucene.analysis.standard.StandardAnalyzer;
import org.apache.lucene.document.Document;
import org.apache.lucene.document.Field;
import org.apache.lucene.index.CorruptIndexException;
import org.apache.lucene.index.IndexWriter;
import org.apache.lucene.store.LockObtainFailedException;

import ca.uwindsor.cs.deepweb.estimation.FilesContentIndexer;

/**
 * Randomly Select given amount of file in a given Directory and send to Lucene
 * to index
 * 
 * @author Liang Jie
 */
public class RandomFilesContentIndexer extends FilesContentIndexer {

	protected Set<Integer> set_offset;

	protected int quality;

	protected int length;

	/**
	 * Constructor
	 * 
	 * @param sourcepath
	 *            The directory that holds the file to be indexed
	 * @param indexdir
	 *            THe directory that store the Lucene index
	 * @param amount
	 *            How many files in the sample index are needed
	 */
	public RandomFilesContentIndexer(String sourcepath, String indexdir,
			int amount) {
		super(sourcepath, indexdir);
		// TODO Auto-generated constructor stub
		set_offset = new HashSet<Integer>();
		quality = amount;
		length = 0;
	}

	/**
	 * Start to index the files in the specified directory. One file's content
	 * is an instance of Document in Lucene.
	 */
	public void generateIndex() {
		try {
			Analyzer luceneAnalyzer = new StandardAnalyzer();
			File[] dataFiles = dataDir.listFiles();
			IndexWriter indexWriter;
			indexWriter = new IndexWriter(indexDir, luceneAnalyzer, true);

			length = dataFiles.length;
			long startTime = new Date().getTime();
			long id = 0;
			int i;
			while (id < quality) {
				i = getRandomOffset();
				if (dataFiles[i].isFile()
						&& dataFiles[i].getName().endsWith(".txt")) {

					Document document = new Document();
					InputStreamReader txtReader = new InputStreamReader(
							new FileInputStream(dataFiles[i]), "UTF-8");

					String word = String.valueOf(id);

					Field FieldPath = new Field(FilesContentIndexer.FIELD_ID,
							word, Field.Store.YES, Field.Index.UN_TOKENIZED);
					Field FieldBody = new Field(
							FilesContentIndexer.FIELD_CONTENT, txtReader);

					document.add(FieldPath);
					document.add(FieldBody);
					indexWriter.addDocument(document);

					id++;
				}
			}
//			System.out.println("index is completed! \n Start to optimize the index...");
//			indexWriter.optimize();
//			System.out.println("optimization is completed!");
			indexWriter.close();
			long endTime = new Date().getTime();

			System.out
					.println("It takes "
							+ (endTime - startTime)
							+ " milliseconds to create index for the files in directory "
							+ dataDir.getPath()
							+ "\nTotal documnents indexed is:"
							+ id);
		} catch (CorruptIndexException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
			System.exit(1);
		} catch (LockObtainFailedException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
			System.exit(1);
		} catch (IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
			System.exit(1);
		}
	}

	protected int getRandomOffset() {
		int index = 0;
		do {
//			double random = ((length - 1) * Math.random());
//			BigDecimal i = new BigDecimal(random).setScale(0,
//					BigDecimal.ROUND_HALF_UP);
//			index = Integer.parseInt(i.toString());
			Random r = new Random();
			index = r.nextInt(length);
		} while (set_offset.contains(index));
		set_offset.add(index);
		return index;

	}
}
